﻿using AuSys.Utils.DependencyInjection;
using FluentValidation;
using System.Reflection;

namespace AuSys.Repos
{
    public static class AddToServices
    {
        public static IServiceCollection AddRepoServices(
            this IServiceCollection services)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type repoType = typeof(IRepo);
            Type lazyType = typeof(LazyInject<>);
            var repos = assembly.GetTypes()
                .Where(repoType.IsAssignableFrom)
                .Where(x => !x.IsAbstract)
                .ToList();
            repos.ForEach(t =>
            {
                services.AddScoped(t);
                var lazyT = lazyType.MakeGenericType(t);
                services.AddScoped(lazyT);
            });
            return services;
        }
    }
}
